package com.example.home;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FunctionCallback;
import com.parse.ParseCloud;
import com.parse.ParseException;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class TypeActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_type);

		Intent sender = getIntent();
		final String value = sender.getExtras().getString("value");
		final String type = sender.getExtras().getString("type");

		final ListView typeItemsList = (ListView) findViewById(R.id.typeListOptions);

		ArrayAdapter<String> typeArrayAdapter = new ArrayAdapter<String>(
				TypeActivity.this, R.layout.statistic_type_option,
				R.id.type_name);

		typeItemsList.setAdapter(typeArrayAdapter);

		typeItemsList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				TextView name = (TextView) view.findViewById(R.id.type_name);

				if (type.equals("income")) {
					addIncome(value, name.getText().toString());
				} else {
					if (type.equals("outcome")) {
						addOutcome(value, name.getText().toString());
					}
				}

				TypeActivity.this.finish();
			}
		});

		if (type.equals("income")) {
			String[] iTypes = new String[] { "Приход - текущ",
					"Приход - следващ" };
			for (String item : iTypes) {
				typeArrayAdapter.add(item);
			}
		} else {
			String[] oTypes = new String[] { "Мтел", "Глобул", "Мегалан",
					"Газ", "Ток", "Вода", "Храна", "Здраве", "Автомобил",
					"Забавление", "Битови", "Такси", "Дрехи", "Кредит" };
			for (String item : oTypes) {
				typeArrayAdapter.add(item);
			}
		}
	}

	private void addIncome(String value, String type) {
		HashMap<String, Object> income = new HashMap<String, Object>();
		income.put("value", Integer.parseInt(value));
		income.put("type", type);
		ParseCloud.callFunctionInBackground("addIncome", income,
				new FunctionCallback<Object>() {

					@Override
					public void done(Object arg0, ParseException arg1) {
						Toast toast;
						if (arg1 == null) {
							toast = Toast.makeText(TypeActivity.this,
									"Успех: Записът беше направен!", Toast.LENGTH_SHORT);
							
						}
						else
						{
							toast = Toast.makeText(TypeActivity.this,
									"Неуспех: Записът НЕ беше направен!", Toast.LENGTH_SHORT);
						}
						toast.show();
					}
				});
	}

	private void addOutcome(String value, String type) {
		HashMap<String, Object> income = new HashMap<String, Object>();
		income.put("value", Integer.parseInt(value));
		income.put("type", type);
		ParseCloud.callFunctionInBackground("addOutcome", income,
				new FunctionCallback<Object>() {

					@Override
					public void done(Object arg0, ParseException arg1) {
						Toast toast;
						if (arg1 == null) {
							toast = Toast.makeText(TypeActivity.this,
									"Успех: Записът беше направен!", Toast.LENGTH_SHORT);
							
						}
						else
						{
							toast = Toast.makeText(TypeActivity.this,
									"Неуспех: Записът НЕ беше направен!", Toast.LENGTH_SHORT);
						}
						toast.show();
					}
				});
	}
}
